layui.use(['layer', 'form', 'table', 'admin', 'ax', 'func', 'laydate', 'upload','InsertSelect'], function () {
    var $ = layui.$;
    var layer = layui.layer;
    var form = layui.form;
    var table = layui.table;
    var $ax = layui.ax;
    var admin = layui.admin;
    var func = layui.func;
    var laydate = layui.laydate;
    var upload = layui.upload;
    var  insertSelect = layui.InsertSelect;

    laydate.render({
        elem: '#timeLimit',
        trigger: 'click'
    });

    insertSelect.render({ elem: '#yyjlqy', data: []});

    /**
     * 检索区域
     */
    var qyAjax = new $ax("/area/getAreaByType");
    qyAjax.set("type", 2);
    var qyResult = qyAjax.start();
    $.each(qyResult, function (index, data) {
        $("#provinceName").append("<option value='" + data.simpleName + "'>" + data.simpleName + "</option>");
    });
    form.render('select');
    form.on('select(provinceName)', function (data) {
        var provinceId = data.value;
        if (provinceId == "") {
            $("#cityName").html("");
            $("#cityName").append("<option value=''>全部</option>");
            form.render('select');
        } else {
            var qyAjax = new $ax("/area/getAreaByPid");
            qyAjax.set("pid", provinceId);
            var qyResult = qyAjax.start();
            $("#cityName").html("");
            $("#cityName").append("<option value=''>全部</option>");
            $.each(qyResult, function (index, data) {
                $("#cityName").append("<option value='" + data.id + "'>" + data.simpleName + "</option>");
            });
            form.render('select');
        }
    });

    /**
     * 检索营运经理
     */
    var languageAjax = new $ax(Feng.ctxPath + "/btyTxl/selectYy");
    var languageResult = languageAjax.start();
    $.each(languageResult, function (index, val) {
        $("#yyjlqy").append("<option value='" + val.yyjlqy + "'>" + val.yyjlqy + "</option>");
    });
    $("#yytel").val(languageResult[0].yytel);
    form.render('select');

    form.on('select(yyjlqy)', function (data) {
        var regionOne = data.value;
        if (regionOne == "") {
            $("#yytel").html("");
            form.render('select');
        } else {
            var qyAjax = new $ax(Feng.ctxPath + "/btyTxl/selectYy");
            qyAjax.set("yyjl", regionOne);
            var qyResult = qyAjax.start();
            $("#yytel").val(qyResult[0].yytel);
            /*$.each(qyResult, function (index, val) {
                $("#yytel").val(val[0].yytel);
            });*/
            form.render('select');
        }
    });

    // 表单提交事件
    $("#submit").on('click', function () {
        let obj = {};
        obj.csqy = $("#provinceName").val();
        obj.yyjlqy = $("#yyjlqy").val();
        obj.yytel = $("#yytel").val();
        obj.ctbh = $("#ctbh").val();
        obj.dm = $("#dm").val();
        obj.kysj = $("#timeLimit").val();
        obj.ctdh = $("#ctdh").val();
        obj.ctdz = $("#ctdz").val();
        if(obj.ctbh!=""&&obj.dm!=""&&obj.kysj!=""){
            $.ajax({
                type: "post",
                url: "/btyTxl/addct",
                data: obj,
                async: false,
                success: function (date) {
                    if (date == 1) {
                        Feng.error("该店已存在，请勿重复添加！！");
                    } else if (date == 2) {
                        Feng.error("餐厅编号重复！！");
                    }else if (date == 0) {
                        Feng.success("添加成功!");
                        admin.putTempData('formOk', true);
                        admin.closeThisDialog();
                    }
                }, error: function (date) {
                    Feng.error("添加失败！" + date);
                    return false;
                }
            });
        }
    })
});